﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class AreaContratoRepository:MasterRepository, IAreaContratoRepository
    {
        public List<AREA_CONTRATO> GetAllFromAreaContrato()
        {
            var query = from ac in context.AREA_CONTRATOs
                        select ac;

            return query.ToList();
        }

        public List<AREA_CONTRATO> GetAllFromAreaContratoByDescripcion(string descripcion)
        {
            var query = from ac in context.AREA_CONTRATOs
                        select ac;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from ac in context.AREA_CONTRATOs
                        where ac.Descripcion.ToUpper().Contains(descripcion)
                        select ac;
            }
            return query.ToList();
        }

        public AREA_CONTRATO GetFromAreaContratoById(int id)
        {
            var query = from ac in context.AREA_CONTRATOs
                        where ac.IdAreaContrato.Equals(id)
                        select ac;
            return query.SingleOrDefault();
        }

        public void AddAreaContrato(AREA_CONTRATO areaContrato)
        {
            context.AREA_CONTRATOs.Add(areaContrato);
            context.SaveChanges();
        }

        public void UpdateAreaContrato(AREA_CONTRATO areaContrato)
        {
            context.Entry(areaContrato).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteAreaContrato(int id)
        {
            var existe = context.AREA_CONTRATOs.Find(id);

            if (existe != null)
            {
                context.AREA_CONTRATOs.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from ac in context.AREA_CONTRATOs
                        where ac.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        select ac;
            return query.Any();
        }
    }
}
